using System;
using System.Text;
using System.Runtime.InteropServices;

namespace DisplayDemo
{
    public class IniFileHelper
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


        public static void IniWriteValue(string section, string key, object value, string path)
        {
            WritePrivateProfileString(section, key, value.ToString(), path);
        }

        public static string IniReadValue(string section, string key, string path)
        {
            StringBuilder temp = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", temp, 255, path);
            return temp.ToString();
        }

        public static string IniReadValue(string section, string key, string def, string path)
        {
            StringBuilder temp = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", temp, 255, path);

            string val = temp.ToString();

            return String.IsNullOrEmpty(val) ? def : val;
        }
    }
}
